﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuatToanDAO;
using System.Data.SqlClient;
using System.Data;

public partial class Admin_Items : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["UserName"] == null))
        {
            Response.Redirect("Default.aspx?path=" + Request.Url.ToString());
        }
        
        if (!IsPostBack)
        {
            Panel_Add.Visible = false;
            Panel_Edit.Visible = false;

            

            CategoryDAO cate = new CategoryDAO();
            DataTable dt = cate.ExecuteSelectALL().Tables[0];

            cbbCategoryList.DataSource = dt;
            cbbCategoryList.DataBind();
            
            CategoryList_Add.DataSource = dt;
            CategoryList_Add.DataBind();

            CategoryList_Edit.DataSource = dt;
            CategoryList_Edit.DataBind();

            ItemDAO item = new ItemDAO();
            GridView1.DataSource = item.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
            GridView1.DataBind();

        }
        
        
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {

        int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

        ItemDAO item = new ItemDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("ItemID", itemid));
        parameters.Add(new SqlParameter("CategoryID", CategoryList_Edit.SelectedValue));
        parameters.Add(new SqlParameter("Name", txt_EditTitle.Text));
        parameters.Add(new SqlParameter("Description", txtDescription_Edit.Text));
        parameters.Add(new SqlParameter("Demo", txtDemo_Edit.Text));
        parameters.Add(new SqlParameter("Other1", cbb_TabActive_Edit.SelectedValue));
        parameters.Add(new SqlParameter("Other2", ""));

        item.ExecuteUpdate(parameters);
        cbbCategoryList.SelectedValue = CategoryList_Edit.SelectedValue;
        cbbCategoryList_SelectedIndexChanged(null, null);

        lbl_Message.Text = "Edit successfully!";
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        ItemDAO item = new ItemDAO();
        DataTable dt = item.ExecuteSelectFollowItemID(itemid).Tables[0];

        CategoryList_Edit.SelectedValue = dt.Rows[0]["CategoryID"].ToString();
        txt_EditTitle.Text = dt.Rows[0]["Name"].ToString();
        txtDescription_Edit.Text = dt.Rows[0]["Description"].ToString();
        txtDemo_Edit.Text = dt.Rows[0]["Demo"].ToString();
        cbb_TabActive_Edit.SelectedValue = dt.Rows[0]["Other1"].ToString();

        Panel_Add.Visible = false;
        Panel_Edit.Visible = true;
    }
    protected void btn_AddItems_Click(object sender, EventArgs e)
    {
        Panel_Add.Visible = true;
        CategoryList_Add.SelectedValue = cbbCategoryList.SelectedValue;

        Panel_Edit.Visible = false;
        

    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        ItemDAO item = new ItemDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();

        int viewcount = 0;

        parameters.Add(new SqlParameter("CategoryID", CategoryList_Add.SelectedValue));
        parameters.Add(new SqlParameter("Name", txt_AddTitle.Text));
        parameters.Add(new SqlParameter("Description", txtDescription_Add.Text));
        parameters.Add(new SqlParameter("Demo", txtDemo_Add.Text));
        parameters.Add(new SqlParameter("Other1", cbb_TabActive_Add.SelectedValue));
        parameters.Add(new SqlParameter("Other2", ""));
        parameters.Add(new SqlParameter("CreateDate", DateTime.Now));
        parameters.Add(new SqlParameter("ViewCount", viewcount));

        item.ExecuteInsert(parameters);
        cbbCategoryList.SelectedValue = CategoryList_Add.SelectedValue;
        cbbCategoryList_SelectedIndexChanged(null, null);
        
        reset();
        lbl_Message.Text = "Add successfully!";

    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

        ItemDAO item = new ItemDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("ItemID", itemid));
        item.ExecuteDelete(parameters);

        lbl_Message.Text = "Delete successfully!";

        cbbCategoryList.SelectedValue = CategoryList_Edit.SelectedValue;
        cbbCategoryList_SelectedIndexChanged(null, null);
        Panel_Edit.Visible = false;
    }

    protected void reset()
    {
        txt_AddTitle.Text = "";
        txtDemo_Add.Text = "";
        txtDescription_Add.Text = "";
        txtDemo_Add.Text = "";
        cbb_TabActive_Add.SelectedIndex = 0;
    }
    protected void cbbCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string categoryid = cbbCategoryList.SelectedValue.Trim();

        ItemDAO item = new ItemDAO();

        GridView1.DataSource = item.ExecuteSelectFollowCategoryID(categoryid).Tables[0];
        GridView1.DataBind();
        Panel_Edit.Visible = false;
        Panel_Add.Visible = false;
    }
    
}